﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Revit.MEP.Opening
{
	public partial class DlgAddDrivepipe : System.Windows.Forms.Form
	{
		public DlgAddDrivepipe(WindowHandle wndRevit)
		{
			this.m_nDrivepipeType = 0;
			this.m_nSelPipeType = 0;
			this.m_bSelPipeTypeChange = false;
			this.m_hWndRevit = wndRevit;
			this.InitializeComponent();
		}

		public int DrivepipeType
		{
			get
			{
				return this.m_nDrivepipeType;
			}
		}

		public int SelPipeType
		{
			get
			{
				return this.m_nSelPipeType;
			}
		}

		public bool SelPipeTypeChange
		{
			get
			{
				return this.m_bSelPipeTypeChange;
			}
			set
			{
				this.m_bSelPipeTypeChange = value;
			}
		}

		private void rdoDrivePipeType1_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = 0;
		}

		private void rdoDrivePipeType2_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = 1;
		}

		private void rdoDrivePipeType3_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = 2;
		}

		private void rdoDrivePipeType4_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = 3;
		}

		private void rdoDrivePipeType5_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = 4;
		}

		private void rdoDrivePipeType6_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = 5;
		}

		private void rdoDrivePipeType7_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = 6;
		}

		private void rdoDrivePipeType8_CheckedChanged(object sender, EventArgs e)
		{
			this.m_nDrivepipeType = 7;
		}

		private void rdoSelPipeType1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_nSelPipeType != 0)
			{
				this.m_nSelPipeType = 0;
				this.SelPipeTypeChange = true;
				this.CancelCommand();
			}
		}

		private void rdoSelPipeType2_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_nSelPipeType != 1)
			{
				this.m_nSelPipeType = 1;
				this.SelPipeTypeChange = true;
				this.CancelCommand();
			}
		}

		private void DlgAddDrivePipe_Load(object sender, EventArgs e)
		{
			this.ReadDlgData();
			if (this.m_nDlgPosX < 0 || this.m_nDlgPosX > 3000)
			{
				this.m_nDlgPosX = 0;
			}
			if (this.m_nDlgPosY < 0 || this.m_nDlgPosY > 3000)
			{
				this.m_nDlgPosY = 0;
			}
			base.Location = new System.Drawing.Point(this.m_nDlgPosX, this.m_nDlgPosY);
			this.rdoDrivePipeType1.Checked = (this.m_nDrivepipeType == 0);
			this.rdoDrivePipeType2.Checked = (this.m_nDrivepipeType == 1);
			this.rdoDrivePipeType3.Checked = (this.m_nDrivepipeType == 2);
			this.rdoDrivePipeType4.Checked = (this.m_nDrivepipeType == 3);
			this.rdoDrivePipeType5.Checked = (this.m_nDrivepipeType == 4);
			this.rdoDrivePipeType6.Checked = (this.m_nDrivepipeType == 5);
			this.rdoDrivePipeType7.Checked = (this.m_nDrivepipeType == 6);
			this.rdoDrivePipeType8.Checked = (this.m_nDrivepipeType == 7);
			this.rdoSelPipeType1.Checked = (this.m_nSelPipeType == 0);
			this.rdoSelPipeType2.Checked = (this.m_nSelPipeType == 1);
			base.Icon = Resource.YArchitechIcon;
		}

		private void DlgAddDrivePipe_FormClosed(object sender, FormClosedEventArgs e)
		{
			System.Drawing.Point location = base.Location;
			this.m_nDlgPosX = location.X;
			this.m_nDlgPosY = location.Y;
			this.WriteDlgData();
			this.CancelCommand();
		}

		private void ReadDlgData()
		{
			string path = Path.GetTempPath() + "AddDrivePipe.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.m_nDlgPosX = Convert.ToInt32(streamReader.ReadLine());
			this.m_nDlgPosY = Convert.ToInt32(streamReader.ReadLine());
			this.m_nDrivepipeType = Convert.ToInt32(streamReader.ReadLine());
			this.m_nSelPipeType = Convert.ToInt32(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		private void WriteDlgData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "AddDrivePipe.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.m_nDlgPosX);
			streamWriter.WriteLine(this.m_nDlgPosY);
			streamWriter.WriteLine(this.m_nDrivepipeType);
			streamWriter.WriteLine(this.m_nSelPipeType);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void CancelCommand()
		{
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgAddDrivepipe.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgAddDrivepipe.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			DlgAddDrivepipe.SendMessage(this.m_hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private int m_nDlgPosX;

		private int m_nDlgPosY;

		private int m_nDrivepipeType;

		private int m_nSelPipeType;

		private bool m_bSelPipeTypeChange;

		private WindowHandle m_hWndRevit;
	}
}
